<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\DevItem;
use Encore\Admin\Widgets\Table;
use App\Admin\Actions\CreateApp;

class DevItemController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '开发项目管理';
	/*
	public function index(Content $content){
		$model = new DevItem;
		$app = $model->with('apps')->find(1);
		dd($app->toArray());
	}*/
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DevItem);

        $grid->column('id', __('ID'))->width('30');
		$grid->column('name', __('名称'))->width('100')->expand(function ($model) {
			$apps = $model->apps()->get()->map(function ($app) {
				return $app->only(['name', 'title', 'created_at']);
			});
			return new Table(['名称', '标题', '发布时间'], $apps->toArray());
		});
		$grid->column('_desc', __('项目介绍'))->display(function () {
			$desc = '<p><span class="pull-right">更新: '.$this->updated_at.'</span><b>'.$this->title.'</b><span style="margin-left:30px;" class="label label-info">'.$this->ver.'</span></p>';
			$desc .= '<p>作者：'.$this->author.'';
			$desc .= !empty($this->email) ? '<span style="margin-left:60px;">邮箱：<a href="mailto:'.$this->email.'">'.$this->author.'</a></span>' : '';
			$desc .= !empty($this->homepage) ? '<span style="margin-left:60px;">网站：<a href="'.$this->homepage.'" target="_balnk">'.$this->homepage.'</a></span>' : '';
			$desc .= '</p>'.$this->desc;
			return $desc;
		});
       
		$grid->column('status', __('状态'))->bool()->width('50');
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->add(new CreateApp);
		});
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DevItem::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new DevItem);

        $form->display('id', __('ID'));		
		$form->text('name', __('项目标识'))
			->creationRules('required|unique:items,name|regex:/^[a-z][a-z_]+$/',['regex' => '标识必须是英文开头且只包含小写字母、下划线'])
			->updateRules('required|unique:items,name,{{name}}|regex:/^[a-z][a-z_]+$/',['regex' => '标识必须是英文开头且只包含小写字母、下划线']);
		$form->text('title', __('项目名称'))->rules('required');
		$form->textarea('desc', __('项目介绍'))->rules('required');
		$form->text('ver', __('当前版本'))->rules('required');
		$form->text('author', __('项目作者'))->rules('required');
		$form->text('email', __('联系邮箱'))->rules('email|nullable');
		$form->text('homepage', __('网站首页'))->rules('url|nullable');
		$form->switch('status', __('启用状态'))->default(1);
        $form->datetime('updated_at', __('更新日期'));

        return $form;
    }
}
